<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class category_model extends CI_Model	 {
	function __construct() {
		// Call the Model constructor
		parent::__construct();
		$this->load->database();
	}
	function get_all_category() {
		$this->db->order_by("iID","DESC");
		$query = $this->db->get('tbl_category');
		if ($query->num_rows() > 0) {
			return $query->result();
		}
		return $query->result();
	}
	function get_root_category($id) {
		$this->db->order_by("iID","DESC");
		$this->db->where("iID !=",$id);
		$query = $this->db->get('tbl_category');
		if ($query->num_rows() > 0) {
			return $query->result();
		}
		return $query->result();
	}
	function add_category($category_name, $category_root,$category_status){
		$data = array(
				'sCateName'=>$category_name,
				'iCateRoot'=>$category_root,
				'iStatus'=>$category_status
		);
		$this->db->insert('tbl_category', $data);
	}
	function get_category_by_id ($id) {
		$this->db->where('iID',$id);
		$query = $this->db->get('tbl_category');
		if ($query->num_rows() > 0) {
			return $query->result();
		}
		return $query->result();
	}
	function edit_category($id, $catename, $cateroot) {
		$data = array('sCateName' => $catename,'iCateRoot'=>$cateroot);
		$this->db->where('iID', $id);
		$this->db->update('tbl_category', $data);
	}
	function del_category($id) {
		$this->db->where('iID', $id);
		$this->db->or_where('iCateRoot',$id);
		$this->db->delete('tbl_category');
	}
}